package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

import static com.example.tests.GroupDataGenerator.generateRandomString;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length<3){
			System.out.println("Please specify parameters: <amount of test data> <file> format");
			return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()){
			System.out.println("File exist, please remove " + file + " manually");
			return;
		}
		
		List<ContactData> groups = generateRandomContacts(amount);
		if ("csv".equals(format)){
			saveGroupsToCsvFile(groups, file);
		} else if ("xml".equals(format)){
			saveGroupsToXmlFile(groups, file);
		} else {
			System.out.println("Unknown Format " + format);
			return;
		}
	}

	private static void saveGroupsToXmlFile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	
	public static List<ContactData> loadContactsFromXmlFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	private static void saveGroupsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstName() + ","+ contact.getLastName() + "," + contact.getAddress() + "," +
					contact.getHomeTelephone() + "," + contact.getHomeTelephone() + "," + contact.getMobileTelephone() + "," + 
					contact.getWorkTelephone() + "," + contact.getEmail() + "," + contact.getSecondEmail() + "," + contact.getSecondAddress()
					+ "," + contact.getSecondHomeTelephone() + "," + contact.getDayBirthday() + "," + contact.getMonthBirthday() + "," + contact.getYearBirthday()+ "\n");
		}
		writer.close();
	}
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contactData = new ContactData()
        	.withFirstName(part[0])
        	.withLastName(part[1])
        	.withAddress(part[2])
        	.withHomeTelephone(part[3])
        	.withMobileTelephone(part[4])
        	.withWorkTelephone(part[5])
        	.withEmail(part[6])
        	.withSecondEmail(part[7])
        	.withSecondAddress(part[8])
        	.withSecondHomeTelephone(part[9])
        	.withDayBirthday(part[10])
        	.withMonthBirthdat(part[11])
        	.withYearBirthday(part[12]);
			list.add(contactData);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i<amount; i++){
            ContactData contact = new ContactData()
            	.withFirstName(generateRandomString())
            	.withLastName(generateRandomString())
            	.withAddress(generateRandomString())
            	.withHomeTelephone(generateRandomString())
            	.withMobileTelephone(generateRandomString())
            	.withWorkTelephone(generateRandomString())
            	.withEmail(generateRandomString())
            	.withSecondEmail(generateRandomString())
            	.withSecondAddress(generateRandomString())
            	.withSecondHomeTelephone(generateRandomString())
            	.withDayBirthday(generateRandomDay())
            	.withMonthBirthdat(generateRandomMonth())
            	.withYearBirthday(generateRandomYear());
            list.add(contact);
		}
		return list;
	}
	
	public static String generateRandomDay(){
		Random rnd = new Random();
		int day = rnd.nextInt(31);
		if (day == 0){
			return "-";
		} else {
			return ""+ day;
		}
	}
	
	public static String generateRandomYear(){
		Random rnd = new Random();
		int year = rnd.nextInt(2030);
		if (year == 0){
			return "";
		} else {
			return ""+ year;
		}
	}
	
	public static String generateRandomMonth(){
		Random rnd = new Random();
		DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
		int month = rnd.nextInt(12);
		if (month == 12){
			return "";
		} else {
			return ""+ dfs.getMonths()[month];
		}
	}
	
}
